package org.queueify;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

abstract public class QueueifyPushQueueTask extends QueueifyTask {
	
	final static private Logger logger = Logger.getLogger(QueueifyPushQueueTask.class.getSimpleName());
	
	private long countdownMillis;
	
	private HttpServletRequest req;
	private HttpServletResponse resp;

	abstract public void doTask() throws QueueifyRetryException;

	final public void doTask(HttpServletRequest req, HttpServletResponse resp) {
		this.req = req;
		this.resp = resp;
		try {
			doTask();
		}
		catch (QueueifyRetryException e) {
			logger.info(e.getMessage());
			resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // this will cause the task to be tried again, obeying the Retry backoff rules.
		}
	}

	public int getRetryCount() {
		return Integer.parseInt(req.getHeader("X-AppEngine-TaskRetryCount"));
	}

	public HttpServletRequest getHttpServletRequest() {
		return req;
	}
	public HttpServletResponse getHttpServletResponse() {
		return resp;
	}
	public void countdownMillis(long countdownMillis) {
		this.countdownMillis = countdownMillis;
	}
	public long getCountdownMillis() {
		return countdownMillis;
	}

}
